<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>pools.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_events_pools.js.html">pools.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2005 Google Inc. All Rights Reserved
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Helper object to manage the event system pools. This should not
<a name="line17"></a> * be used by itself and there should be no reason for you to depend on this
<a name="line18"></a> * library.
<a name="line19"></a> *
<a name="line20"></a> * JScript 5.6 has some serious issues with GC so we use object pools to reduce
<a name="line21"></a> * the number of object allocations.
<a name="line22"></a> *
<a name="line23"></a> */
<a name="line24"></a>
<a name="line25"></a>goog.provide(&#39;goog.events.pools&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>goog.require(&#39;goog.events.BrowserEvent&#39;);
<a name="line29"></a>goog.require(&#39;goog.events.Listener&#39;);
<a name="line30"></a>goog.require(&#39;goog.structs.SimplePool&#39;);
<a name="line31"></a>goog.require(&#39;goog.userAgent.jscript&#39;);
<a name="line32"></a>
<a name="line33"></a>
<a name="line34"></a>/**
<a name="line35"></a> * Helper function for returning an object that is used for the lookup trees.
<a name="line36"></a> * This might use an object pool depending on the script engine.
<a name="line37"></a> * @return { {count_: number, remaining_: number} } A new or reused object.
<a name="line38"></a> */
<a name="line39"></a>goog.events.pools.getObject;
<a name="line40"></a>
<a name="line41"></a>
<a name="line42"></a>/**
<a name="line43"></a> * Helper function for releasing an object that was returned by
<a name="line44"></a> * {@code goog.events.pools.getObject}. In case an object pool was used the
<a name="line45"></a> * object is returned to the pool.
<a name="line46"></a> * @param { {count_: number, remaining_: number} } obj The object to release.
<a name="line47"></a> */
<a name="line48"></a>goog.events.pools.releaseObject;
<a name="line49"></a>
<a name="line50"></a>
<a name="line51"></a>/**
<a name="line52"></a> * Helper function for returning an array.
<a name="line53"></a> * This might use an object pool depending on the script engine.
<a name="line54"></a> * @return {Array} A new or reused array.
<a name="line55"></a> */
<a name="line56"></a>goog.events.pools.getArray;
<a name="line57"></a>
<a name="line58"></a>
<a name="line59"></a>/**
<a name="line60"></a> * Helper function for releasing an array that was returned by
<a name="line61"></a> * {@code goog.events.pools.getArray}. In case an object pool was used the
<a name="line62"></a> * array is returned to the pool.
<a name="line63"></a> * @param {Array} arr The array to release.
<a name="line64"></a> */
<a name="line65"></a>goog.events.pools.releaseArray;
<a name="line66"></a>
<a name="line67"></a>
<a name="line68"></a>/**
<a name="line69"></a> * Helper function for returning a proxy function as needed by
<a name="line70"></a> * {@code goog.events}. This might use an object pool depending on the script
<a name="line71"></a> * engine.
<a name="line72"></a> * @return {Function} A new or reused function object.
<a name="line73"></a> */
<a name="line74"></a>goog.events.pools.getProxy;
<a name="line75"></a>
<a name="line76"></a>
<a name="line77"></a>/**
<a name="line78"></a> * Sets the callback function to use in the proxy.
<a name="line79"></a> * @param {Function} cb The callback function to use.
<a name="line80"></a> */
<a name="line81"></a>goog.events.pools.setProxyCallbackFunction;
<a name="line82"></a>
<a name="line83"></a>
<a name="line84"></a>/**
<a name="line85"></a> * Helper function for releasing a function that was returned by
<a name="line86"></a> * {@code goog.events.pools.getProxy}. In case an object pool was used the
<a name="line87"></a> * function is returned to the pool.
<a name="line88"></a> * @param {Function} f The function to release.
<a name="line89"></a> */
<a name="line90"></a>goog.events.pools.releaseProxy;
<a name="line91"></a>
<a name="line92"></a>
<a name="line93"></a>/**
<a name="line94"></a> * Helper function for returning a listener object as needed by
<a name="line95"></a> * {@code goog.events}. This might use an object pool depending on the script
<a name="line96"></a> * engine.
<a name="line97"></a> * @return {goog.events.Listener} A new or reused listener object.
<a name="line98"></a> */
<a name="line99"></a>goog.events.pools.getListener;
<a name="line100"></a>
<a name="line101"></a>
<a name="line102"></a>/**
<a name="line103"></a> * Helper function for releasing a listener object that was returned by
<a name="line104"></a> * {@code goog.events.pools.getListener}. In case an object pool was used the
<a name="line105"></a> * listener object is returned to the pool.
<a name="line106"></a> * @param {goog.events.Listener} listener The listener object to release.
<a name="line107"></a> */
<a name="line108"></a>goog.events.pools.releaseListener;
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>/**
<a name="line112"></a> * Helper function for returning a {@code goog.events.BrowserEvent} object as
<a name="line113"></a> * needed by {@code goog.events}. This might use an object pool depending on the
<a name="line114"></a> * script engine.
<a name="line115"></a> * @return {!goog.events.BrowserEvent} A new or reused event object.
<a name="line116"></a> */
<a name="line117"></a>goog.events.pools.getEvent;
<a name="line118"></a>
<a name="line119"></a>
<a name="line120"></a>/**
<a name="line121"></a> * Helper function for releasing a browser event object that was returned by
<a name="line122"></a> * {@code goog.events.pools.getEvent}. In case an object pool was used the
<a name="line123"></a> * browser event object is returned to the pool.
<a name="line124"></a> * @param {goog.events.BrowserEvent} event The event object to release.
<a name="line125"></a> */
<a name="line126"></a>goog.events.pools.releaseEvent;
<a name="line127"></a>
<a name="line128"></a>
<a name="line129"></a>(function() {
<a name="line130"></a>  var BAD_GC = goog.userAgent.jscript.HAS_JSCRIPT &amp;&amp;
<a name="line131"></a>      !goog.userAgent.jscript.isVersion(&#39;5.7&#39;);
<a name="line132"></a>
<a name="line133"></a>  // These functions are shared between the pools&#39; createObject functions and
<a name="line134"></a>  // the non pooled versions.
<a name="line135"></a>
<a name="line136"></a>  function getObject() {
<a name="line137"></a>    return {count_: 0, remaining_: 0};
<a name="line138"></a>  }
<a name="line139"></a>
<a name="line140"></a>  function getArray() {
<a name="line141"></a>    return [];
<a name="line142"></a>  }
<a name="line143"></a>
<a name="line144"></a>  /**
<a name="line145"></a>   * This gets set to {@code goog.events.handleBrowserEvent_} by events.js.
<a name="line146"></a>   * @type {function(string, (Event|undefined))}
<a name="line147"></a>   */
<a name="line148"></a>  var proxyCallbackFunction;
<a name="line149"></a>
<a name="line150"></a>  goog.events.pools.setProxyCallbackFunction = function(cb) {
<a name="line151"></a>    proxyCallbackFunction = cb;
<a name="line152"></a>  };
<a name="line153"></a>
<a name="line154"></a>  function getProxy() {
<a name="line155"></a>    // Use a local var f to prevent one allocation.
<a name="line156"></a>    var f = function(eventObject) {
<a name="line157"></a>      return proxyCallbackFunction.call(f.src, f.key, eventObject);
<a name="line158"></a>    };
<a name="line159"></a>    return f;
<a name="line160"></a>  }
<a name="line161"></a>
<a name="line162"></a>  function getListener() {
<a name="line163"></a>    return new goog.events.Listener();
<a name="line164"></a>  }
<a name="line165"></a>
<a name="line166"></a>  function getEvent() {
<a name="line167"></a>    return new goog.events.BrowserEvent();
<a name="line168"></a>  }
<a name="line169"></a>
<a name="line170"></a>  if (!BAD_GC) {
<a name="line171"></a>
<a name="line172"></a>    goog.events.pools.getObject = getObject;
<a name="line173"></a>    goog.events.pools.releaseObject = goog.nullFunction;
<a name="line174"></a>
<a name="line175"></a>    goog.events.pools.getArray = getArray;
<a name="line176"></a>    goog.events.pools.releaseArray = goog.nullFunction;
<a name="line177"></a>
<a name="line178"></a>    goog.events.pools.getProxy = getProxy;
<a name="line179"></a>    goog.events.pools.releaseProxy = goog.nullFunction;
<a name="line180"></a>
<a name="line181"></a>    goog.events.pools.getListener = getListener;
<a name="line182"></a>    goog.events.pools.releaseListener = goog.nullFunction;
<a name="line183"></a>
<a name="line184"></a>    goog.events.pools.getEvent = getEvent;
<a name="line185"></a>    goog.events.pools.releaseEvent = goog.nullFunction;
<a name="line186"></a>
<a name="line187"></a>  } else {
<a name="line188"></a>
<a name="line189"></a>    goog.events.pools.getObject = function() {
<a name="line190"></a>      return objectPool.getObject();
<a name="line191"></a>    };
<a name="line192"></a>
<a name="line193"></a>    goog.events.pools.releaseObject = function(obj) {
<a name="line194"></a>      objectPool.releaseObject(obj);
<a name="line195"></a>    };
<a name="line196"></a>
<a name="line197"></a>    goog.events.pools.getArray = function() {
<a name="line198"></a>      return /** @type {Array} */ (arrayPool.getObject());
<a name="line199"></a>    };
<a name="line200"></a>
<a name="line201"></a>    goog.events.pools.releaseArray = function(obj) {
<a name="line202"></a>      arrayPool.releaseObject(obj);
<a name="line203"></a>    };
<a name="line204"></a>
<a name="line205"></a>    goog.events.pools.getProxy = function() {
<a name="line206"></a>      return /** @type {Function} */ (proxyPool.getObject());
<a name="line207"></a>    };
<a name="line208"></a>
<a name="line209"></a>    goog.events.pools.releaseProxy = function(obj) {
<a name="line210"></a>      proxyPool.releaseObject(getProxy());
<a name="line211"></a>    };
<a name="line212"></a>
<a name="line213"></a>    goog.events.pools.getListener = function() {
<a name="line214"></a>      return /** @type {goog.events.Listener} */ (
<a name="line215"></a>          listenerPool.getObject());
<a name="line216"></a>    };
<a name="line217"></a>
<a name="line218"></a>    goog.events.pools.releaseListener = function(obj) {
<a name="line219"></a>      listenerPool.releaseObject(obj);
<a name="line220"></a>    };
<a name="line221"></a>
<a name="line222"></a>    goog.events.pools.getEvent = function() {
<a name="line223"></a>      return /** @type {goog.events.BrowserEvent} */ (eventPool.getObject());
<a name="line224"></a>    };
<a name="line225"></a>
<a name="line226"></a>    goog.events.pools.releaseEvent = function(obj) {
<a name="line227"></a>      eventPool.releaseObject(obj);
<a name="line228"></a>    };
<a name="line229"></a>
<a name="line230"></a>    /**
<a name="line231"></a>     * Initial count for the objectPool
<a name="line232"></a>     */
<a name="line233"></a>    var OBJECT_POOL_INITIAL_COUNT = 0;
<a name="line234"></a>
<a name="line235"></a>
<a name="line236"></a>    /**
<a name="line237"></a>     * Max count for the objectPool_
<a name="line238"></a>     */
<a name="line239"></a>    var OBJECT_POOL_MAX_COUNT = 600;
<a name="line240"></a>
<a name="line241"></a>
<a name="line242"></a>    /**
<a name="line243"></a>     * SimplePool to cache the lookup objects. This was implemented to make IE6
<a name="line244"></a>     * performance better and removed an object allocation in goog.events.listen
<a name="line245"></a>     * when in steady state.
<a name="line246"></a>     */
<a name="line247"></a>    var objectPool = new goog.structs.SimplePool(OBJECT_POOL_INITIAL_COUNT,
<a name="line248"></a>                                                 OBJECT_POOL_MAX_COUNT);
<a name="line249"></a>    objectPool.setCreateObjectFn(getObject);
<a name="line250"></a>
<a name="line251"></a>
<a name="line252"></a>    /**
<a name="line253"></a>     * Initial count for the arrayPool
<a name="line254"></a>     */
<a name="line255"></a>    var ARRAY_POOL_INITIAL_COUNT = 0;
<a name="line256"></a>
<a name="line257"></a>
<a name="line258"></a>    /**
<a name="line259"></a>     * Max count for the arrayPool
<a name="line260"></a>     */
<a name="line261"></a>    var ARRAY_POOL_MAX_COUNT = 600;
<a name="line262"></a>
<a name="line263"></a>
<a name="line264"></a>    /**
<a name="line265"></a>     * SimplePool to cache the type arrays. This was implemented to make IE6
<a name="line266"></a>     * performance better and removed an object allocation in goog.events.listen
<a name="line267"></a>     * when in steady state.
<a name="line268"></a>     * @type {goog.structs.SimplePool}
<a name="line269"></a>     */
<a name="line270"></a>    var arrayPool = new goog.structs.SimplePool(ARRAY_POOL_INITIAL_COUNT,
<a name="line271"></a>                                                ARRAY_POOL_MAX_COUNT);
<a name="line272"></a>    arrayPool.setCreateObjectFn(getArray);
<a name="line273"></a>
<a name="line274"></a>
<a name="line275"></a>    /**
<a name="line276"></a>     * Initial count for the proxyPool
<a name="line277"></a>     */
<a name="line278"></a>    var HANDLE_EVENT_PROXY_POOL_INITIAL_COUNT = 0;
<a name="line279"></a>
<a name="line280"></a>
<a name="line281"></a>    /**
<a name="line282"></a>     * Max count for the proxyPool
<a name="line283"></a>     */
<a name="line284"></a>    var HANDLE_EVENT_PROXY_POOL_MAX_COUNT = 600;
<a name="line285"></a>
<a name="line286"></a>
<a name="line287"></a>    /**
<a name="line288"></a>     * SimplePool to cache the handle event proxy. This was implemented to make
<a name="line289"></a>     * IE6 performance better and removed an object allocation in
<a name="line290"></a>     * goog.events.listen when in steady state.
<a name="line291"></a>     */
<a name="line292"></a>    var proxyPool = new goog.structs.SimplePool(
<a name="line293"></a>        HANDLE_EVENT_PROXY_POOL_INITIAL_COUNT,
<a name="line294"></a>        HANDLE_EVENT_PROXY_POOL_MAX_COUNT);
<a name="line295"></a>    proxyPool.setCreateObjectFn(getProxy);
<a name="line296"></a>
<a name="line297"></a>
<a name="line298"></a>    /**
<a name="line299"></a>     * Initial count for the listenerPool
<a name="line300"></a>     */
<a name="line301"></a>    var LISTENER_POOL_INITIAL_COUNT = 0;
<a name="line302"></a>
<a name="line303"></a>
<a name="line304"></a>    /**
<a name="line305"></a>     * Max count for the listenerPool
<a name="line306"></a>     */
<a name="line307"></a>    var LISTENER_POOL_MAX_COUNT = 600;
<a name="line308"></a>
<a name="line309"></a>
<a name="line310"></a>    /**
<a name="line311"></a>     * SimplePool to cache the listener objects. This was implemented to make
<a name="line312"></a>     * IE6 performance better and removed an object allocation in
<a name="line313"></a>     * goog.events.listen when in steady state.
<a name="line314"></a>     */
<a name="line315"></a>    var listenerPool = new goog.structs.SimplePool(LISTENER_POOL_INITIAL_COUNT,
<a name="line316"></a>                                                   LISTENER_POOL_MAX_COUNT);
<a name="line317"></a>    listenerPool.setCreateObjectFn(getListener);
<a name="line318"></a>
<a name="line319"></a>
<a name="line320"></a>    /**
<a name="line321"></a>     * Initial count for the eventPool
<a name="line322"></a>     */
<a name="line323"></a>    var EVENT_POOL_INITIAL_COUNT = 0;
<a name="line324"></a>
<a name="line325"></a>
<a name="line326"></a>    /**
<a name="line327"></a>     * Max count for the eventPool
<a name="line328"></a>     */
<a name="line329"></a>    var EVENT_POOL_MAX_COUNT = 600;
<a name="line330"></a>
<a name="line331"></a>
<a name="line332"></a>    /**
<a name="line333"></a>     * SimplePool to cache the event objects. This was implemented to make IE6
<a name="line334"></a>     * performance better and removed an object allocation in
<a name="line335"></a>     * goog.events.handleBrowserEvent_ when in steady state.
<a name="line336"></a>     * This pool is only used for IE events.
<a name="line337"></a>     */
<a name="line338"></a>    var eventPool = new goog.structs.SimplePool(EVENT_POOL_INITIAL_COUNT,
<a name="line339"></a>                                                EVENT_POOL_MAX_COUNT);
<a name="line340"></a>    eventPool.setCreateObjectFn(getEvent);
<a name="line341"></a>  }
<a name="line342"></a>})();
</pre>


</body>
</html>
